<template>
    <div class="login">
        <div class="login-cen">
            <h1>登录</h1>
            <h3>七尾医疗服务后台管理系统</h3>
            <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
                <div class="login-box1">
                    <p>账号</p>
                    <el-form-item prop="number">
                        <el-input v-model="ruleForm.number" placeholder="请输入你的账号"></el-input>
                    </el-form-item>
                </div>
                <div class="login-box2">
                    <p>密码</p>
                    <el-form-item prop="password">
                        <el-input show-password maxlength="6" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
                    </el-form-item>

                </div>
            </el-form>
            <div class="box2">
                <p>忘记密码请联系管理员</p>
            </div>
            <el-button type="warning" @click="goLogin" :disabled="loading">登录</el-button>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { FormInstance, FormRules,ElMessage} from 'element-plus'
import {login} from "../../api/login"
import { useRouter } from 'vue-router';

const ruleFormRef = ref<FormInstance>()
const loading=ref<Boolean>(false)
const router=useRouter()

//账号输入框的校验规则
const checknumber = (rule: any, value: any, callback: any) => {
    if (value === '') {
        //没有输入内容时
        callback(new Error('请输入你的账号'))
    }
}
//密码输入框的校验规则
const checkpassword = (rule: any, value: any, callback: any) => {
    if (value === '') {
        //没有输入内容时
        callback(new Error('请输入你的密码'))
    }
}
//数据类型
const ruleForm = reactive({
    number: '',
    password: '',
})

const rules = reactive<FormRules>({
    //账号的校验
    number: [{ validator: checknumber, trigger: 'blur' }],
    //密码的校验
    password: [{ validator: checkpassword, trigger: 'blur' }],
})
//登录

const goLogin = () => {
    // console.log(ruleForm)
    loading.value=true
    if (ruleForm.number && ruleForm.password) {
        console.log("1212")
        login(ruleForm.number,ruleForm.password).then((res)=>{
            console.log(res)
            localStorage.setItem('token',res.data.token)
            router.push("/home")
        }).finally(()=>{
            //不管成功还是失败，都会执行的回调
            //将loading.value设置为false,表示接口调用完成
            loading.value=false
        })
        /**
         * 优化描述：
         *  上面的登录接口，需要在.then中判断业务状态码是否为'200'
         *   后续的所有接口，都需要有这么一段判断逻辑，过于麻烦，可以将这段判断逻辑写入到axios封装的响应拦截中
         */
    } else {
        ElMessage({
            showClose: true,
            message: '请重新输入',
            type: 'error',
        })
    }
}

</script>
<style lang="scss" scoped>
.login {
    height: 100%;
    background-color: green;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-cen {
        width: 300px;
        height: 300px;
        background: blue;
        text-align: center;
        color: #fff;
        padding: 20px;

        .box2 {
            display: flex;
            justify-content: end;
            font-size: 14px;
        }
    }
}

button,
[type="button"],
[type="reset"],
[type="submit"] {
    width: 100%;
    margin-top: 10px;
}

.login-box1,
.login-box2 {
    display: flex;
    height: 30px;
    align-items: center;

    p {
        width: 40px;
        color: black;
        font-weight: bold;
        margin-right: 20px;
    }
}

.login-box2 {
    margin-top: 20px;
}

.el-form-item {
    margin-bottom: 0;
}
</style>